package com.code.controller;


import com.code.domain.CommonResult;
import com.code.domain.GpmartUtil;
import com.code.domain.PageObject;
import com.code.entity.User;
import com.code.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 商城用户信息表 前端控制器
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2022-08-23
 */
@Controller
@RequestMapping("/user")
@Slf4j
@ApiOperation(value = "用户信息",notes = "用户信息")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping(value = "/to/toUserIndex")
    @ApiOperation(value = "跳转用户页面", notes = "跳转用户页面")
    public String toUserIndex(Model model, HttpServletRequest request, HttpServletResponse response,String token){
        return "user/user_index";
    }

    @GetMapping(value = "/queryUserLayUiPage")
    @ApiOperation(value = "前端框架分页查询",notes = "前端框架分页查询")
    public @ResponseBody CommonResult queryUserLayUiPage(PageObject pageObject, User user, HttpServletRequest request, HttpServletResponse response){

        List<User> countList = userService.queryUserList(user);
        List<User> list = userService.findUserPage(GpmartUtil.PageBefore(pageObject.getPage(),pageObject.getLimit()),pageObject.getLimit(),user);
        return new CommonResult(0,"操作成功",countList.size(),list);
    }

    @ApiOperation(value = "跳转到新增用户界面",notes = "跳转到新增用户界面")
    @RequestMapping("/to/toAddUser")
    public String toAddUser(Model model,  HttpServletRequest request, HttpServletResponse response)
    {
        return "user/user_add";
    }

    @ApiOperation(value = "新增用户",notes = "新增用户")
    @PostMapping("/addUserLayUi")
    public String addUserLayUi( User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        int result  =  userService.addUserLayUi(user);
        if(result == 1){
            return "redirect:/login/to/toLoginIndex";
        }else{
            throw new RuntimeException("新增用户失败");
        }
    }

}
